package com.feng.jmx.server;

import java.io.IOException;
import java.rmi.registry.LocateRegistry;
import java.util.Hashtable;

import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.jmx.HierarchyDynamicMBean;
import org.apache.log4j.jmx.LoggerDynamicMBean;

import com.sun.jdmk.comm.HtmlAdaptorServer;

public class HelloAgent {
	static Logger logger = Logger.getLogger(HelloAgent.class);

	public static void main(String[] args) throws Exception {
		jmxServerAgent();
	}

	public static void jmxServerAgent() throws MalformedObjectNameException, NullPointerException, InstanceAlreadyExistsException, MBeanRegistrationException,
			NotCompliantMBeanException, IOException, InterruptedException {
		int rmiPort = 1099;
		String jmxServerName = "TestJMXServer";

		// jdkfolder/bin/rmiregistry.exe 9999
		//Registry registry = 
		LocateRegistry.createRegistry(rmiPort);

		MBeanServer mbs = MBeanServerFactory.createMBeanServer(jmxServerName);
		// MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
		
		/**
		 * HtmlAdaptorServer
		 */
		/*HtmlAdaptorServer adapter = new HtmlAdaptorServer();
		ObjectName adapterName;
		adapterName = new ObjectName(jmxServerName + ":name=" + "htmladapter");
		adapter.setPort(8082);
		adapter.start();
		mbs.registerMBean(adapter, adapterName);*/

		/**
		 * HelloWorld
		 */
		/*ObjectName objName = new ObjectName(jmxServerName + ":name=" + "HelloWorld");
		mbs.registerMBean(new Hello(), objName);
		*/
		/**
		 * Log4j
		 */
		/*Logger rootLogger = LogManager.getRootLogger();
		LoggerDynamicMBean mbean = new LoggerDynamicMBean(rootLogger);
		// List list = MBeanServerFactory.findMBeanServer(null);
		// MBeanServer server = (MBeanServer)list.iterator().next();
		ObjectName objectName = null;
		Hashtable<String, String> nameParameters = new Hashtable<String, String>();
		nameParameters.put("logger", "root");
		try {
			objectName = new ObjectName("mytest", nameParameters);
			mbs.registerMBean(mbean, objectName);
		} catch (Exception e) {
			logger.error("Problems registering Log4J MBean", e);
		}
		*/
		/*ObjectName logName2 = new ObjectName("imc:name=AbstractDynamicMBean"); 
		mbs.registerMBean(new HierarchyDynamicMBean(), logName2);*/
		ObjectName logName = new ObjectName("imc:name=LoggerLevelSet");
		mbs.registerMBean(new LoggerDynamicMBean(LogManager.getLogger("com")), logName);
		
		
		/*Logger rootLogger = LogManager.getRootLogger();
        LoggerDynamicMBean mbean = new LoggerDynamicMBean(rootLogger);
        ObjectName rootLog = new ObjectName(jmxServerName + ":name=" + "rootLogger");
        mbs.registerMBean(mbean, rootLog);*/
        
		JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:" + rmiPort + "/" + jmxServerName);
		System.out.println("JMXServiceURL: " + url.toString());
		JMXConnectorServer jmxConnServer = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
		jmxConnServer.start();
		System.out.println("start.....");
		
		while (true) {
			logger.debug("test");
			logger.info("test");
			logger.warn("test");
			logger.error("test");
			Thread.sleep(5000);
		}
	}

	public static void jmxServerAgent2() throws MalformedObjectNameException, NullPointerException, InstanceAlreadyExistsException, MBeanRegistrationException,
			NotCompliantMBeanException, InterruptedException {
		MBeanServer server = MBeanServerFactory.createMBeanServer();
		HtmlAdaptorServer adapter = new HtmlAdaptorServer();

		ObjectName adapterName = new ObjectName("HelloAgent:name=htmladapter,port=8082");
		server.registerMBean(adapter, adapterName);
		/*
		 * ObjectName helloName = new ObjectName("imc:name=HelloWorld");
		 * server.registerMBean(new Hello(), helloName);
		 * 
		 * //log ObjectName logName = new ObjectName("imc:name=LoggerLevelSet");
		 * server.registerMBean(new LoggerDynamicMBean(logger), logName);
		 * 
		 * ObjectName logName2 = new
		 * ObjectName("imc:name=AbstractDynamicMBean"); server.registerMBean(new
		 * HierarchyDynamicMBean(), logName2);
		 */
		Logger rootLogger = LogManager.getRootLogger();
		LoggerDynamicMBean mbean = new LoggerDynamicMBean(rootLogger);
		// List list = MBeanServerFactory.findMBeanServer(null);
		// MBeanServer server = (MBeanServer)list.iterator().next();

		ObjectName objectName = null;
		Hashtable<String, String> nameParameters = new Hashtable<String, String>();
		nameParameters.put("logger", "root");
		try {
			objectName = new ObjectName("mytest", nameParameters);
			server.registerMBean(mbean, objectName);
		} catch (Exception e) {
			logger.error("Problems registering Log4J MBean", e);
		}
		adapter.start();
		System.out.println("start.....");
		while (true) {
			logger.debug("test");
			logger.info("test");
			logger.warn("test");
			logger.error("test");
			Thread.sleep(5000);
		}
	}
}
